Set Interface


Set
set는 순서와 상관없이 중복을 허용하는 data structure 이다.
    HashSet class
HashSetTest.java
package collection.hashset;
import java.util.HashSet;
public class HashSetTest {
public static void main(String[] args) {
HashSet<String> hashSet=new HashSet<String>();
hashSet.add(new String(""));
hashSet.add(new String(""));
hashSet.add(new String(""));
hashSet.add(new String(""));
hashSet.add(new String(""));
System.out.println(hashSet);
}
}

[홍연의, 박현정, 강감찬, 임정순]

MemberHashSet.java
package collection.hashset;
import java.util.HashSet;
import java.util.Iterator;
import collection.Member;
public class MemberHashSet {
private HashSet<Member> hashSet;
public MemberHashSet() {
hashSet=new HashSet<Member>();
}
public void addMember(Member member) {
hashSet.add(member);
}
public boolean removeMember(int memberId) {
Iterator<Member> ir=hashSet.iterator();
while(ir.hasNext()) {
Member member=ir.next();
int tempId=member.getMemberId();
if(tempId==memberId) {
hashSet.remove(member);
return true;
}
}
System.out.println(memberId+" .");
return false;
}
public void showAllMember() {
for(Member member:hashSet) {
System.out.println(member);
}
System.out.println();
}
}
MemberHashSetTest.java
package collection.hashset;
import collection.Member;
public class MemberHashSetTest {
public static void main(String[] args) {
MemberHashSet memberHashSet=new MemberHashSet();
Member memberLee=new Member(1001,"");
Member memberSon=new Member(1002,"");
Member memberPark=new Member(1003,"");
memberHashSet.addMember(memberLee);
memberHashSet.addMember(memberSon);
memberHashSet.addMember(memberPark);
memberHashSet.showAllMember();
Member memberHong=new Member(1003,"");
memberHashSet.addMember(memberHong);
memberHashSet.showAllMember();
}
}

이지원회원님의 아이디는 1001 입니다.

손민국회원님의 아이디는 1002 입니다.

박서훤회원님의 아이디는 1003 입니다.


이지원회원님의 아이디는 1001 입니다.

손민국회원님의 아이디는 1002 입니다.

박서훤회원님의 아이디는 1003 입니다.

because we @Override equals and hashCode function at Member.java
same memberId also be measure.
    TreeSet
java Collection interface or Map interface 에서 Tree로 시작하는 class는 데이터를 추가한 후 출력하면,
결과값이 정렬되어서 출력된다.
TreeSetTest.java
package collection.hashset;
import java.util.TreeSet;
public class TreeSetTest {
public static void main(String[] args) {
TreeSet<String> treeSet=new TreeSet<String>();
treeSet.add("");
treeSet.add("");
treeSet.add("");
for(String str:treeSet) {
System.out.println(str);
}
}
}

강감찬

이순신

홍길동

MemberTreeSet.java
package collection.treeset;
import java.util.TreeSet;
import java.util.Iterator;
import collection.Member;
public class MemberTreeSet {
private TreeSet<Member> treeSet;
public MemberTreeSet() {
treeSet=new TreeSet<Member>();
}
public void addMember(Member member) {
treeSet.add(member);
}
public boolean removeMember(int memberId) {
Iterator<Member> ir=treeSet.iterator();
while(ir.hasNext()) {
Member member=ir.next();
int tempId=member.getMemberId();
if(tempId==memberId) {
treeSet.remove(member);
return true;
}
}
System.out.println(memberId+" .");
return false;
}
public void showAllMember() {
for(Member member:treeSet) {
System.out.println(member);
}
System.out.println();
}
}
MemberTreeSetTest.java
package collection.treeset;
import collection.Member;
public class MemberTreeSetTest {
public static void main(String[] args) {
MemberTreeSet memberTreeSet=new MemberTreeSet();
Member memberPark=new Member(1003,"");
Member memberLee=new Member(1001,"");
Member memberSon=new Member(1002,"");
memberTreeSet.addMember(memberLee);
memberTreeSet.addMember(memberLee);
memberTreeSet.addMember(memberSon);
memberTreeSet.showAllMember();
Member memberHong=new Member(1003,"");
memberTreeSet.addMember(memberHong);
memberTreeSet.showAllMember();
}
}

이지원회원님의 아이디는 1001 입니다.

손민국회원님의 아이디는 1002 입니다.


이지원회원님의 아이디는 1001 입니다.

손민국회원님의 아이디는 1002 입니다.

홍길동회원님의 아이디는 1003 입니다.

inorder to use Member with TreeSet must defined order criteria in Member(Comparable & compareTo)